@extends('admin.app')

@section('breadcrumb')
    <div class="bg-light lter b-b wrapper-md">
        <ol class="breadcrumb bg-light lter m-n no-padder">
            <li><a href="{{ admin_action('HomeController@index') }}">主页</a></li>
            <li>网站管理</li>
            <li><a href="{{ admin_action('Web\MessagesController@index') }}">消息管理</a></li>
            <li class="active">新增消息</li>
        </ol>
    </div>
@endsection

@section('content')
    <div class="panel panel-default">
        <div class="panel-heading font-bold">
            消息表单
        </div>
        <div class="panel-body">
            <form  class="form-horizontal form-validation"
                  action="{{ admin_action('Web\MessagesController@store') }}"
                  method="post">
                {{ csrf_field() }}
                <div class="form-group">
                    <label class="col-lg-2 control-label">标题*</label>
                    <div class="col-lg-10">
                        <input type="text" class="form-control" name="title" value="{{ old('title') }}"
                               placeholder="请输入消息标题" data-rule-required="true" data-msg-required="消息 怎么可以没有标题呢 O(∩_∩)O哈哈~"
                               data-rule-maxlength="100" data-msg-maxlength="消息 标题不宜太长,以100个字符为宜">
                    </div>
                </div>
                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">消息类型*</label>
                    <div class="col-lg-10">
                        <select class="select2 form-control" name="type" data-rule-required="true"
                                data-msg-required="这是一条什么消息  ??(^_^)!!">
                            <option value="">无</option>
                            @foreach($message_types as $message_type)
                                <option value="{{ $message_type->code }}">{{ $message_type->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">消息发送者*</label>
                    <div class="col-lg-4">
                        <input type="text" class="form-control" name="announcer" value="{{ old('announcer')??$author }}"
                               placeholder="请填写发送者姓名" data-rule-required="true"
                               data-msg-required="优秀的文章怎么可以没有作者呢 O(∩_∩)O哈哈~"
                               data-rule-maxlength="50" data-msg-maxlength="作者的名字课没有那么长哦 O(∩_∩)O哈哈~,做多50个字符">
                    </div>
                </div>
                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">内容*</label>
                    <div class="col-lg-10">
                        <script id="ueditor_container" name="content" type="text/plain">作为一个有情怀的人,总是能够写出优秀的文章! ^_^#

                        </script>
                    </div>
                </div>
                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">发布时间*</label>
                    <div class="col-lg-4">
                        <input type="text" class="form-control datetimepicker" name="published_at"
                               value="{{ old('published_at') }}" placeholder="请选择发布时间" data-rule-required="true"
                               readonly="readonly">
                    </div>
                </div>
                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">接收人类型*</label>
                    <div class="col-lg-10">
                        <select class="select2 form-control select-receiver-type" name="receiver_type" data-rule-required="true"
                                data-msg-required="请选择接收人类型">
                            <option value="">请选择接收人类型</option>
                            @foreach($receiver_types as $receiver_type)
                                <option value="{{ $receiver_type->code }}" {{ $receiver_type->code=="all_users"?"selected=selected":"" }}>{{ $receiver_type->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group select-receiver">
                    <label class="col-lg-2 control-label">指定接收人*</label>
                    <div class="col-lg-10">
                        <select id="receiver_ids"  data-request-url="{{ admin_action('Web\MessagesController@query_receiver') }}" name="receiver_ids[]" class="js-data-example-ajax form-control select2" multiple data-rule-required="true"></select>
                    </div>
                </div>

                <div class="line line-dashed b-b line-lg"></div>
                <div class="form-group">
                    <div class="col-lg-4 col-lg-offset-2">
                        <a href="{{ admin_action('Web\MessagesController@index') }}"
                           class="btn btn-default">返&nbsp;回</a>
                        <button type="submit" class="btn btn-primary">保&nbsp;存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection
@section('scripts')
    @include('admin.partials.ueditor')

    <script>
        $(function () {
            //初始化默认为当前时间
            $('.datetimepicker').val(formatDate(new Date()));
            //---------------------------------select2 ajax------------------------------------//

            $(".js-data-example-ajax").select2({
                placeholder: "请输入姓名和身份证模糊查询...",
                language:'zh-CN',
                ajax: {
                    url: $("#receiver_ids").attr("data-request-url"),
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            query_str: params.term, // search term
                            page: params.page
                        };
                    },
                    results: function (data) {
                        return {results: data};
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data,
                            pagination: {
                                more: (params.page * 30) < data.total_count
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 1,
                formatInputTooShort: "请输入用户名",
                formatNoMatches: "没有匹配的用户",
                formatSearching: "查询中..."
            });
        });



        /*------------------------------------------------------------function--------------------------------------------*/

        /**
         * 格式化时间
         *
         * @param now
         * @returns {string}
         */
        function formatDate(now) {
            var year = now.getFullYear();
            var month = now.getMonth() + 1;
            var date = now.getDate();
            var hour = now.getHours();
            var minute = now.getMinutes();
            var second = now.getSeconds();
            return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
        };


        /*-------------------------------------------选择接收人-----------------------------------------------------*/
        //默认隐藏接收人表单


        $(".select-receiver-type").change(function(){
            if($(this).val()=="select_users"){
                //取消隐藏接收人表单
                $(".select-receiver").show();
            }else{
                //隐藏接收人表单
                $(".select-receiver").hide();
            }
        });

        document.onreadystatechange = function () {
            if(document.readyState=="complete") {
                $(".select-receiver").hide();
            }
        }
    </script>@endsection